
import { _decorator, Component, Node, Prefab, EventMouse, Sprite, UITransform, Vec3, Vec2, Graphics, Color, find } from 'cc';
import { GameConfig } from '../gameConfig';
import { ColorMem } from '../gamecore/colorMem';
import { FogGoGame } from '../gamecore/fogGoGame';
import { MVec2 } from '../gamecore/MVect2';
import { ResultMsg } from '../gamecore/resultMsg';
import { TEST_PLACED_RESULT } from '../gamecore/testPlacedResult';
import { TestSocketioClien } from '../netio/testSocketioClien';
import { FogManager } from './fogManager';
import { TryPla } from './tryPla';
const { ccclass, property } = _decorator;


//当鼠标移入棋盘，触发事件 
@ccclass('FloatColor')
export class FloatColor extends Component {
    @property(Node)
    white: Node = null; //白棋子

    @property(Node)
    black: Node = null; //黑棋
    
    @property(Node)
    fogs: Node = null;

    fogManager: FogManager = null;

    fogGo: FogGoGame;

    private last: Vec2 = null;

    g: Graphics;

    yimaPiece: number = ColorMem.C_WHITE;

    socketio: TestSocketioClien = null;

    onLoad () {
        this.node.on(Node.EventType.MOUSE_MOVE, this.plafloat, this);
        this.fogGo = new FogGoGame(GameConfig.instance.gameSize);
        this.g = this.getComponent(Graphics);
        this.fogManager = this.fogs.getComponent(FogManager);
        //测试
        this.socketio = TestSocketioClien.instance; 
    }

    plafloat(evt: EventMouse) {
        let uiLocationV3 = new Vec3(evt.getUILocation().x, evt.getUILocation().y, 0);
        let v = this.getCakeNodeByTouchPos(uiLocationV3);
        //越界，鼠标移除棋盘
        if (v.x < 0 || v.y < 0 || v.x>= GameConfig.instance.gameSize || v.y >= GameConfig.instance.gameSize) {
            this.white.active = false;
            this.black.active = false;
            if (this.last != null) {
                this.huabshs(this.last, new Color(0,0,0,255));
                if (this.fogs.active) {
                    this.fogManager.setfogArrActive(this.last, true);
                }
            }
            return;
        }
        //
        if (this.last != null && this.last.equals(v)) {
            return;
        }
       
        
         //TODO: 待修改，接入接口
         this.socketio.tryPlay(new MVec2(v.x,v.y), function(rs: ResultMsg){
             if (!rs.Success) {
                 this.socketio.lable.string = rs.Message;
                 return;
             }
             if (this.fogs.active) {
                this.fogManager.setfogArrActive(v, false);
            }

        });
        if (this.fogGo.floatPlaced(v) == TEST_PLACED_RESULT.N_NX) {
            console.log(this.fogs.active);
            
            if (this.last != null) {
                this.huabshs(this.last, new Color(0,0,0,255)); //把之前线花
                if (this.fogs.active) {
                    this.fogManager.setfogArrActive(this.last, true);
                }
            }
            if (this.fogs.active) {
                this.fogManager.setfogArrActive(v, false);
            }
            if (this.yimaPiece == ColorMem.C_BLACK) {
                this.huabshs(v, new Color(255,255,255,255));
                this.black.getComponent(UITransform).anchorPoint = this.lgPointToAnchorPoint(v);
                let tryPla = this.black.getChildByName("tryPla").getComponent(TryPla);
                tryPla.clearFill();
                tryPla.upFill(this.getUILocation(v));
                this.black.active = true;
            } else if (this.yimaPiece == ColorMem.C_WHITE) {
                this.huabshs(v, new Color(255,255,255,255));
                this.white.getComponent(UITransform).anchorPoint = this.lgPointToAnchorPoint(v);
                let tryPla = this.white.getChildByName("tryPla").getComponent(TryPla);
                tryPla.clearFill();
                tryPla.upFill(this.getUILocation(v));
                this.white.active = true;
            }
        }
        this.last = v;
    }

    getCakeNodeByTouchPos(pos: Vec3): Vec2 {
        let offsetPos = this.getComponent(UITransform)!.convertToNodeSpaceAR(pos);
        let x: number = Math.floor((offsetPos.x + 300) / GameConfig.instance.width + 0.5);
        let y: number = Math.floor((offsetPos.y + 300) / GameConfig.instance.width + 0.5);
        return new Vec2(x, y);
    }

    getUILocation(v: Vec2): Vec2 {
        let x: number = v.x * GameConfig.instance.width - 300
        let y: number = v.y * GameConfig.instance.width - 300
        return new Vec2(x, y);
    }

    lgPointToAnchorPoint(v: Vec2): Vec2 {
        return new Vec2(9.5 - v.x, 9.5 - v.y);
    }

    huabshs(v: Vec2, color: Color) {
        let v1 = this.getUILocation(v);
        this.g.strokeColor = color;
        this.g.moveTo(v1.x, -300);
        this.g.lineTo(v1.x, 300);
        this.g.moveTo(-300, v1.y);
        this.g.lineTo(300, v1.y);
        this.g.stroke();
    }
}
